package com.UdpRecieve.framework.listenner;


import com.UdpRecieve.framework.constant.ServletConstant;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.Date;

@WebListener
public class SysHttpSessionListener implements HttpSessionListener {

    Logger logger = LoggerFactory.getLogger(SysHttpSessionListener.class);

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        logger.info("session创建了"+ DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        logger.info("session销毁了"+ DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        //如果销毁的是指定用户的sessionid,就把该用户移出在线用户列表中
        ServletConstant.OnlineUserList.entrySet().removeIf(entry -> entry.getKey() != se.getSession().getId());

    }
}
